from typing import List, Dict

from fastapi import APIRouter

from backend.app.common.response.response_schema import response_base
from backend.app.schemas.device import DeviceDetailReq, DeviceInfoSchema, DeviceDeleteReq
from backend.app.services.device_service import DeviceService

router = APIRouter()


@router.post('/detail', summary='查看商品详情')
async def get_detail(req: DeviceDetailReq):
    detail = await DeviceService().get_detail(req)
    return await response_base.success(data=detail)


# @router.post('/list', summary='查看商品列表')
# async def get_list(req: DeviceDetailReq):
#     lists = DeviceService().get_list(req)
#     return await response_base.success(data=lists)


@router.post('/update', summary='修改商品信息')
async def sync_goods_detail(req: DeviceInfoSchema):
    if not req.gid:
        return await response_base.fail(msg="缺少设备ID")
    _ = await DeviceService().update_info(req)
    return await response_base.success()


@router.post('/batch_add', summary='批量创建向量')
async def batch_add_goods(req: List[DeviceInfoSchema]):
    # 组装数据
    texts: List[str] = []
    metadata: List[Dict] = []
    for info in req:
        if info.gid < 1:
            return await response_base.fail(msg="商品ID不能为空")
        t = info.to_page_content()
        d = info.model_dump(by_alias=True, exclude=set("vector_id", ))
        metadata.append(d)
        texts.append(t)

    res = await DeviceService().abatch_add(texts, metadata)
    return await response_base.success(data=res)


@router.post('/batch_delete', summary='根据向量ID批量删除')
async def batch_delete_goods(req: DeviceDeleteReq):
    if not req.vector_ids:
        return await response_base.fail(msg="请选择您要删除商品的向量ID")

    res = await DeviceService().batch_delete(req.vector_ids, req.device_ids)
    if not res:
        return await response_base.fail(msg="操作失败，请稍后重试")
    return await response_base.success()

